import numpy as np
import newton as new

def elastic_force(k,X0,X):
    return -k*(X-X0)

print elastic_force(2,1,2)


def centrifugal_force(k,X0,Y0,X,Y):
    return np.array([[k*(X-X0)],[k*(Y-Y0)]])

print centrifugal_force(2,1,2,5,7);

def centrifugal_force_d(k, X0, Y0, X, Y):#le jacobien
    n=2;
    resultat = np.zeros((n,n));
    resultat[0,0] = k
    resultat[1,1] = k
    resultat[0,1] = 0
    resultat[1,0] = 0
    return resultat

print centrifugal_force_d(2,1,2,5,7);

def gravitational_force(k,X0,Y0,X,Y):
    return -k/(np.sqrt(np.square(X-X0)+(np.square(Y-Y0))))**3 * np.array([[(X-X0)],[(Y-Y0)]])

print gravitational_force(2,1,2,5,7);#-I2 dans ce cas ?

def gravitational_force_d(k,x0,y0,x,y): #le jacobien
	n=2;
	resultat = np.zeros((n,n));
	resultat[0,0] = -k/((x-x0)**2+(y-y0)**2)**(3/2)+3/k*(x-x0)/((x-x0)**2+(y-y0)**2)**(5/2)*(x-x0);
	resultat[1,1] = -k/((x-x0)**2+(y-y0)**2)**(3/2)+3*k*(y-y0)/((x-x0)**2+(y-y0)**2)**(5/2)*(y-y0);
	resultat[0,1] = 3*k*(x-x0)/((x-x0)**2+(y-y0)**2)**(5/2)*(y-y0);
	resultat[1,0] = 3*k*(y-y0)/((x-x0)**2+(y-y0)**2)**(5/2)*(x-x0);
	return resultat;

print gravitational_force_d(2,1,2,5,7);

# Derives des forces

#------------ Question 2 --------------#

#Methode de Newton-Raphson

#forces gravitationelles avec coef egaux a 1 et 0.01
#remarque : ces forces ne s'appliquent pas au meme point.
def fg(u):
	return gravitational_force(1, -1, -2, u[0], u[1]) + gravitational_force(0.01, 3, 4, u[0], u[1]);
	
def dfg(u):
	return gravitational_force_d(1, -1, -2, u[0], u[1]) + gravitational_force_d(0.01, 3, 4, u[0], u[1]);

print "Force gravitationnelle"
print new.Newton_Raphson_N(fg, dfg, np.array([1,1]), 0.1, 50);



#force centrifuge appliquee au barycentre de deux masses.
#coefficients egaux a 1 => s'applique au milieu de ces deux masses

def fc(u):
    return centrifugal_force(1, 1, 1, u[0], u[1]);

def dfc(u):
    return centrifugal_force_d(1, 1, 1, u[0], u[1]);

print "Force centrifuge"
print new.Newton_Raphson_N(fc, dfc, np.array([1,1]), 0.1, 50);



#----------------  Question 3 --------------

#coder une application


